# -*- coding: utf-8 -*-
# @Time    : 2024/4/23 15:10
# @Author  : floraachy
# @File    : repo_pull_request_detail_page.py
# @Software: PyCharm
# @Desc:

# 标准库导入
from typing import Optional, Union, List
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoPullRequestDetailPage(BasePage):
    """项目-X合并请求详情页"""
    # 合并请求标题
    locator_pr_title_span = "//div[@class='ver-middle']//span[@class='font-18 fwb']"
    # 合并请求内容
    locator_pr_content_p = "//div[@class='main']//div[@class='break_word_comments imageLayerParent markdown-body']//p"
    # 编辑 按钮
    locator_edit_pr_btn = "//span[text()='编 辑']"
    # 拒绝 按钮
    locator_refuse_pr_btn = "//span[text()='拒 绝']"
    # 合并请求被合并/拒绝的页面提示文案
    locator_pr_status_span = "(//span[text()='{}'])[1]"
    # 合并请求状态: 开启的/已合并/已拒绝
    locator_pr_status_tag = "//span[text()='{}']"
    # 源分支
    locator_source_branch_a = "(//span[@class='ant-tag pr-branch-tag']//a)[1]"
    # 添加评论
    locator_add_comment_btn = "//span[text()='添加评论...']/parent::button"
    # 评论内容输入框
    locator_comment_content_input = "//textarea/following::div[contains(@class, 'CodeMirror-wrap')]"
    # 附件上传框
    locator_comment_file_input = "//p[text()='拖动文件或点击此处上传']/parent::div/preceding-sibling::input"
    # 评论按钮
    comment_btn = "//span[text()='评 论']/parent::button"
    # 合并请求按钮
    merge_btn = "//span[text()='合并请求']/parent::button"
    # 合并时 标题输入框
    merge_title_input = "//input[@placeholder='标题']"
    # 合并时，描述输入框
    merge_content_input = "//textarea[@placeholder='请输入合并描述...']"
    # 确认合并按钮
    confirm_merge_btn = "//span[text()='确认合并']/parent::button"

    @allure.step("访问合并请求详情页：/{repo_owner}/{repo_identifier}/pulls/{pull_request_id}")
    def navigate(self, repo_owner, repo_identifier, pull_request_id):
        """
        访问合并请求详情页
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/pulls/{pull_request_id}")

    @allure.step("点击【编辑】按钮")
    def click_edit_button(self):
        """
        点击【编辑】按钮
        """
        self.click(self.locator_edit_pr_btn)

    @allure.step("点击【拒绝】按钮")
    def click_refuse_button(self):
        """
        点击【拒绝】按钮
        """
        self.click(self.locator_refuse_pr_btn)

    @allure.step("输入合并请求评论： {comment}")
    def input_pull_request_comment(self, comment):
        """
        输入合并请求评论
        """
        self.click(self.locator_add_comment_btn)
        self.page.wait_for_timeout(1000)
        elem = self.page.query_selector(selector=self.locator_comment_content_input)
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, comment])

    @allure.step("上传合并请求评论附件: {pr_comment_file}")
    def upload_pr_comment_attachments(self, pr_comment_file: str):
        """
        上传合并请求评论附件
        """
        self.upload_file(locator=self.locator_comment_file_input, file_path=pr_comment_file)

    @allure.step("点击【评论】按钮")
    def click_comment_button(self):
        """
        点击【评论】按钮，提交评论
        """
        self.click(self.comment_btn)

    @allure.step("点击【合并请求】按钮")
    def click_pull_request_button(self):
        """
        点击【合并请求】按钮
        """
        self.click(self.merge_btn)

    @allure.step("输入标题：{title}")
    def input_merge_title(self, title):
        self.input(self.merge_title_input, title)

    @allure.step("输入描述：{desc}")
    def input_merge_desc(self, desc):
        self.input(self.merge_content_input, desc)

    @allure.step("点击【确认合并】，提交合并请求")
    def click_confirm_merge_button(self):
        """
        点击【确认合并】，提交合并请求
        """
        self.click(self.confirm_merge_btn)

    @allure.step("合并请求(PR)详情页，拒绝合并请求")
    def decline_pull_request(self):
        """
        合并请求详情页，拒绝合并请求
        """
        # 点击【拒绝】按钮
        self.click_refuse_button()

    # --------------------- 流程 -------------------------------------

    @allure.step("合并请求(PR)详情页，合并合并请求， 合并标题：{merge_title}， 合并内容：{merge_content}")
    def merge_pull_request_flow(self, merge_title, merge_content):
        """
        合并请求详情页，合并合并请求
        """
        # 点击【合并请求】按钮
        self.click_pull_request_button()
        # 输入标题：${generate_name()}
        self.input_merge_title(merge_title)
        # 输入描述：${generate_paragraph()}
        self.input_merge_desc(merge_content)
        # 点击【确认合并】按钮
        self.click_confirm_merge_button()

    @allure.step(
        "合并请求(PR)详情页，评论合并请求， 评论内容：{comment}， 评论附件：{pr_comment_files}")
    def comment_pull_request_flow(self, comment: str, pr_comment_files: Optional[Union[str, List[str]]] = None):
        """
        合并请求详情页，评论合并请求
        """
        # 输入合并请求评论：${generate_paragraph()}
        self.input_pull_request_comment(comment)

        if pr_comment_files:
            if isinstance(pr_comment_files, list):
                for pr_comment_file in pr_comment_files:
                    self.upload_pr_comment_attachments(pr_comment_file)
            if isinstance(pr_comment_files, str):
                self.upload_pr_comment_attachments(pr_comment_files)
        # 点击【评论】按钮
        self.click_comment_button()
